/*
 * libustream: User-space stackable streams
 * @(#) $Id: strops.c 2 2009-02-20 09:52:11Z mo@ilaven.net $
 */

/*
 * Copyright (c) 2002, 2008 Mo McRoberts.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) of this software may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_libustream.h"

/* us_create()
 * Create a new stream and return its interface pointer. If @oflag includes
 * O_CREAT and creating the stream causes a disk file to be created, that file
 * will be created with the mode specified as @mode; otherwise, the @mode
 * parameter will be ignored.
 */
IUStream *
us_create(const char *iristr, int oflag, mode_t mode)
{
	iri_t *iri;
	IUStream *r;

	if(NULL == (iri = iri_parse(iristr)))
	{
/*		fprintf(stderr, "IRI parsing failed\n"); */
		return NULL;
	}
	r = us_create_iri(iri, oflag, mode);
	iri_destroy(iri);
	return r;
}

IUStream *
us_create_iri(const iri_t *iri, int oflag, mode_t mode)
{
	com_clsid_t clsid;
	IUStreamFactory *factory;
	IUStreamNode *node;
	IUStream *stream;
	int fd;
	com_result_t r;
	size_t c;
	
	if(-1 == us__module_lookup(iri->schemelist[iri->nschemes - 1], &clsid))
	{
		errno = EAFNOSUPPORT;
		return NULL;
	}
	if(COM_S_OK != (r = com_getclass(&clsid, COM_CTX_ALL, NULL, &IID_IUStreamFactory, (void **) &factory)))
	{
		errno = r;
		return NULL;
	}
	if(NULL == (stream = us__stream_create(iri, oflag, mode)))
	{
		return NULL;
	}
	if(COM_S_OK != (r = IUStreamFactory_CreateStreamHead(factory, &clsid, iri, oflag, mode, &node, &fd)))
	{
/*		fprintf(stderr, "Failed to create stream head\n"); */
		IUStream_Release(stream);
		IUStreamFactory_Release(factory);
		errno = r;
		return NULL;
	}
	us__stream_push(stream, node, fd);
	us__fd_associate(fd, stream);
	IUStreamFactory_Release(factory);
	IUStreamNode_Release(node);
	if(iri->nschemes > 1)
	{
		for(c = iri->nschemes - 1; c; c--)
		{
			if(-1 == us__module_lookup(iri->schemelist[c - 1], &clsid))
			{
				IUStream_Release(stream);
				errno = EAFNOSUPPORT;
				return NULL;
			}
			if(COM_S_OK != (r = com_getclass(&clsid, COM_CTX_ALL, NULL, &IID_IUStreamFactory, (void **) &factory)))
			{
				IUStream_Release(stream);
				errno = r;
				return NULL;
			}
			if(COM_S_OK != (r = IUStreamFactory_CreateStreamNode(factory, &clsid, stream, &node)))
			{
				IUStreamFactory_Release(factory);
				IUStream_Release(stream);
				errno = r;
				return NULL;
			}
			us__stream_push(stream, node, fd);
		}
	}
	return stream;
}

int
us_push(IUStream *stream, const char *module)
{
	com_clsid_t clsid;
	IUStreamNode *node;
	IUStreamFactory *factory;
	com_result_t r;
	
	if(-1 == us__module_lookup(module, &clsid))
	{
		return -1;
	}
	if(COM_S_OK != (r = com_getclass(&clsid, COM_CTX_ALL, NULL, &IID_IUStreamFactory, (void **) &factory)))
	{
		errno = r;
		return -1;
	}
	if(COM_S_OK != (r = IUStreamFactory_CreateStreamNode(factory, &clsid, stream, &node)))
	{
		IUStreamFactory_Release(factory);
		errno = r;
		return -1;
	}
	us__stream_push(stream, node, -1);
	IUStreamFactory_Release(factory);
	IUStreamNode_Release(node);
	return 0;
}

int
us_destroy(IUStream *stream)
{
	IUStream_Release(stream);
	return 0;
}

ssize_t
us_put(IUStream *stream, const char *buf, size_t buflen)
{
	com_result_t r;
	size_t orig;
	
	orig = buflen;
	if(COM_S_OK != (r = IUStream_Write(stream, buf, &buflen)))
	{
		errno = r;
		return -1;
	}
	return (ssize_t) (orig - buflen);
}

ssize_t
us_get(IUStream *stream, char *buf, size_t buflen)
{
	com_result_t r;
	size_t orig;
	
	orig = buflen;
	if(COM_S_OK != (r = IUStream_Read(stream, buf, &buflen)))
	{
		if(EPIPE == r)
		{
			return 0;
		}
		errno = r;
		return -1;
	}
	return (ssize_t) (orig - buflen);
}
